<?php

namespace App\Http\Controllers\Admin\ContentManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class ArticleController extends BaseController {

    /**
     * @api {post} /content-manager/article 文章创建
     * @apiVersion 0.1.0
     * @apiName 内容管理-文章创建
     * @apiGroup 内容管理
     *
     *
     * @apiParam {number} sort                          排序 必传
     * @apiParam {number} article_cat_id                分类id 必传
     * @apiParam {string} title                         名称 必传
     * @apiParam {string} content                        内容 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'sort' => 'required|numeric|min:1|max:2100000000',
            'title' => 'required|string|max:25',
            'content' => 'required|string',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $params = $request->input();

        $res = app('repo_content');

        $rows = $res->addArticle($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /content-manager/article 文章删除
     * @apiVersion 0.1.0
     * @apiName 内容管理-文章删除
     * @apiGroup 内容管理
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_content')->delArticle($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /content-manager/article 文章修改
     * @apiVersion 0.1.0
     * @apiName 内容管理-文章修改
     * @apiGroup 内容管理
     *
     *
     * @apiParam {number} id
     * @apiParam {number} sort                          排序 必传
     * @apiParam {number} article_cat_id                分类id 必传
     * @apiParam {string} title                         名称 必传
     * @apiParam {string} content                        内容 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'id' => 'required|integer',
            'sort' => 'required|numeric|min:1|max:2100000000',
            'title' => 'required|string|max:255',
            'content' => 'required|string',
        ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $res = app('repo_content');

        $params = $request->input();
        $id = $params['id'];

        $params = [
            'content' => $params['content'],
            'sort' => $params['sort'],
            'title' => $params['title'],
        ];

        $rows = $res->saveArticle(["article_id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /content-manager/article 文章列表
     * @apiVersion 0.1.0
     * @apiName 内容管理-文章列表
     * @apiGroup 内容管理
     *
     * @apiParam {number} article_cat_id  文章分类id 可选
     * @apiParam {string} keyword 文章标题 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "article_id(序号)": 17,
                "title(文章标题)": "管理规定",
                "content(文章内容)": "<p>用户协议等各类规定</p>",
                "article_cat_id(分类id)": 8,
                "sort(排序)": 0,
                "add_time(添加时间)": "1970-01-01 08:00:00"
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'article_cat_id' => 'filled|numeric|exists:mysql.ln_article_category,article_cat_id',
            'keyword' => 'filled|string',
            'perPage' => 'filled|numeric',
            'page' => 'filled|numeric',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_content')->getArticleQueryList($request->input());

        return Response::sendList($list);
    }


}
